#!/bin/bash
function clearIptables(){
    echo "iptables -F" > /ipf.sh
    if [ $? -ne 0 ]; then
        echo "Failed to generate file."
        return 1
    fi
    echo "Generate file successfully"
    sudo echo "/ipf.sh" >> /etc/rc.d/rc.local
    if [ $? -ne 0 ]; then
        echo "Failed to add scheduled task."
        return 1
    fi
    echo "Successfully added scheduled task"
    sudo chmod +x /etc/rc.d/rc.local
    if [ $? -ne 0 ]; then
        echo "Permission modification failed."
        return 1
    fi
    echo "Successfully started the cleanup rule."
}

function tasks(){
    # Define the command to run
    command="sync; echo 3 > /proc/sys/vm/drop_caches"

    # Define the schedule for the task
    schedule="0 0 * * *"

    # Create the entry in the crontab file
    echo "$schedule $command" | crontab 
    systemctl enable crond 
    systemctl restart crond
}

function optimizeKernel(){
    ulimit -HSn 50000
    echo "" > /etc/sysctl.conf
    echo "net.core.wmem_default =10486760" >> /etc/sysctl.conf
    echo "net.core.wmem_max=26214400" >> /etc/sysctl.conf
    echo "net.core.rmem_default=26214400" >> /etc/sysctl.conf
    echo "net.core.rmem_max=56214400" >> /etc/sysctl.conf
    echo "net.core.netdev_max_backlog = 10000" >> /etc/sysctl.conf
    echo "fs.file-max=500000" >> /etc/sysctl.conf
    echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
    #echo "net.ipv4.tcp_congestion_control=bbrplus=1" >> /etc/sysctl.conf
    sysctl -p
    if [ $? -ne 0 ]; then
        echo "Kernel optimization failed."
        return 1
    fi
    echo "Kernel optimization succeeded."
}


function installTools(){
    echo "Installing tools..."
    sudo yum install git wget mtr iftop iperf3 bind-utils tcptraceroute bc -y
    if [ $? -ne 0 ]; then
        echo "Error installing tools."
        return 1
    fi
    echo "Downloading tcping..."
    sudo wget -O //usr/bin/tcping https://soft.mengclaw.com/Bash/TCP-PING --no-check-certificate
    if [ $? -ne 0 ]; then
        echo "Error downloading tcping."
        return 1
    fi
    echo "Setting executable permissions for tcping..."
    sudo chmod a+x /usr/bin/tcping
    if [ $? -ne 0 ]; then
        echo "Error setting permissions for tcping."
        return 1
    fi
    echo "Tools installation complete."
    return 0
}

function installBBr(){
    sudo echo "$(curl -H "Authorization: Token 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" -L -s https://raw.githubusercontent.com/1443213244/EasyGost3/main/aoutgost3.sh)" >/gost.sh
    if [ $? -ne 0 ]; then
        echo "Error downloading gost script."
        return 1
    fi
    echo "downloading gost script complete."
    sudo chmod +x /gost.sh
    if [ $? -ne 0 ]; then
        echo "Error downloading gost script."
        return 1
    fi
    echo "downloading gost script complete."

    sudo echo "/gost.sh" >> /etc/rc.d/rc.local
    if [ $? -ne 0 ]; then
        echo "Failed to add scheduled task."
        return 1
    fi
    echo "Successfully added scheduled task"

    sudo chmod +x /etc/rc.d/rc.local
    if [ $? -ne 0 ]; then
        echo "Failed to add scheduled task."
        return 1
    fi
    echo "Successfully added scheduled task"
    sudo bash -c "$(curl -H "Authorization: Token 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" -L -s https://raw.githubusercontent.com/1443213244/General_module/main/bbrplus.sh)"
    if [ $? -ne 0 ]; then
        echo "Error installing bbrplus."
        return 1
    fi

    return 0
}

installTools
if [ $? -ne 0 ]; then
    echo "Tools installation failed"
    return 1
fi
echo "Tools installation successful"
optimizeKernel
clearIptables
tasks
installBBr
if [ $? -ne 0 ]; then
    echo "BBR installation failed"
    return 1
fi


echo "success"

exit 0
